package com.spark.incoicingsi.controller;

import com.spark.incoicings.utils.AliOssUtil;
import com.spark.incoicingsi.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/oss")
@Slf4j
public class OssServiceController {

    @Resource
    private AliOssUtil aliOssUtil;
    @PostMapping("/policy")
    public Result upload(MultipartFile file){
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString()+extension;
            String filePath = aliOssUtil.upload(file.getBytes(),
                    objectName);
            return Result.success(200,"success",filePath);
        } catch (IOException e) {
            log.error("文件上传失败,{}",e);
        }
        return Result.error(400,"上传失败");
    }

}
